using UnityEditor;
using UnityEngine;
using System.Collections.Generic;

[CustomEditor(typeof(物品管理器))]
public class 物品管理器Editor : Editor
{
    private bool showItemDictionary = true;
    private Dictionary<string, bool> categoryFoldoutStates = new Dictionary<string, bool>();
    private Dictionary<string, bool> itemIdFoldoutStates = new Dictionary<string, bool>();
    private Dictionary<string, bool> itemDetailFoldoutStates = new Dictionary<string, bool>();
    private Vector2 scrollPosition;

    public override void OnInspectorGUI()
    {
        物品管理器 manager = (物品管理器)target;

        scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);

        showItemDictionary = EditorGUILayout.Foldout(showItemDictionary, "Item Dictionary", true);
        if (showItemDictionary)
        {
            EditorGUI.indentLevel++;

            if (manager.ItemDictionary == null || manager.ItemDictionary.Count == 0)
            {
                EditorGUILayout.LabelField("空");
            }
            else
            {
                // 按类别和ID重新组织数据
                Dictionary<string, Dictionary<string, List<Item>>> categorizedItems = new Dictionary<string, Dictionary<string, List<Item>>>();

                foreach (var outerKvp in manager.ItemDictionary)
                {
                    foreach (var innerKvp in outerKvp.Value)
                    {
                        Item item = innerKvp.Value;
                        if (item == null) continue;

                        string category = item.ItemCategory.ToString();
                        string itemId = item.ItemId;

                        // 确保类别存在
                        if (!categorizedItems.ContainsKey(category))
                        {
                            categorizedItems[category] = new Dictionary<string, List<Item>>();
                            if (!categoryFoldoutStates.ContainsKey(category))
                            {
                                categoryFoldoutStates[category] = false;
                            }
                        }

                        // 确保ID分组存在
                        if (!categorizedItems[category].ContainsKey(itemId))
                        {
                            categorizedItems[category][itemId] = new List<Item>();
                            string itemIdKey = $"{category}_{itemId}";
                            if (!itemIdFoldoutStates.ContainsKey(itemIdKey))
                            {
                                itemIdFoldoutStates[itemIdKey] = false;
                            }
                        }

                        categorizedItems[category][itemId].Add(item);
                    }
                }

                // 按类别显示
                foreach (var categoryKvp in categorizedItems)
                {
                    string category = categoryKvp.Key;

                    // 显示类别折叠
                    categoryFoldoutStates[category] = EditorGUILayout.Foldout(
                        categoryFoldoutStates[category],
                        $"类别: {category} ({categoryKvp.Value.Count}种物品)",
                        true);

                    if (categoryFoldoutStates[category])
                    {
                        EditorGUI.indentLevel++;

                        foreach (var itemIdKvp in categoryKvp.Value)
                        {
                            string itemId = itemIdKvp.Key;
                            string itemIdKey = $"{category}_{itemId}";

                            // 显示物品ID分组折叠
                            itemIdFoldoutStates[itemIdKey] = EditorGUILayout.Foldout(
                                itemIdFoldoutStates[itemIdKey],
                                $"ID: {itemId} ({itemIdKvp.Value.Count}个)",
                                true);

                            if (itemIdFoldoutStates[itemIdKey])
                            {
                                EditorGUI.indentLevel++;

                                for (int i = 0; i < itemIdKvp.Value.Count; i++)
                                {
                                    Item item = itemIdKvp.Value[i];
                                    string itemKey = $"{itemIdKey}_{i}";

                                    if (!itemDetailFoldoutStates.ContainsKey(itemKey))
                                    {
                                        itemDetailFoldoutStates[itemKey] = false;
                                    }

                                    EditorGUILayout.BeginVertical("box");

                                    // 显示单个物品折叠
                                    itemDetailFoldoutStates[itemKey] = EditorGUILayout.Foldout(
                                        itemDetailFoldoutStates[itemKey],
                                        $"{item.ItemName} (物品码: {item.Itemcnt})",
                                        true);

                                    if (itemDetailFoldoutStates[itemKey])
                                    {
                                        EditorGUI.indentLevel++;

                                        EditorGUILayout.LabelField("物品名称:", item.ItemName);
                                        EditorGUILayout.LabelField("物品类别:", item.ItemCategory.ToString());
                                        EditorGUILayout.LabelField("物品ID:", item.ItemId);
                                        EditorGUILayout.LabelField("物品描述:", item.ItemDescription);
                                        EditorGUILayout.Toggle("能否堆叠:", item.ItemCanStacked);
                                        EditorGUILayout.IntField("数量:", item.ItemQuantity);
                                        EditorGUILayout.IntField("物品码:", item.Itemcnt);

                                        EditorGUI.indentLevel--;
                                    }

                                    EditorGUILayout.EndVertical();
                                }

                                EditorGUI.indentLevel--;
                            }
                        }

                        EditorGUI.indentLevel--;
                    }
                }
            }

            EditorGUI.indentLevel--;
        }

        EditorGUILayout.EndScrollView();

        EditorGUILayout.Space();
        if (GUILayout.Button("刷新显示"))
        {
            categoryFoldoutStates.Clear();
            itemIdFoldoutStates.Clear();
            itemDetailFoldoutStates.Clear();
            Repaint();
        }


    }



}